#!/bin/bash
#
# run.sh: Stuff that's not part of the doc main process.
#
# Usage:
#   ./run.sh <action> ...

pyg() {
  pygmentize -f html -l python <<EOF
def foo():
  pass
EOF
}

# http://pygments.org/docs/quickstart/
# Their styles are embedded in Python code -- I want CSS.

style-py() {
  python <<EOF
import pygments
from pygments.formatters import HtmlFormatter

print HtmlFormatter().get_style_defs('.highlight')

EOF
}


# Use this to generate the checked in style sheet.
style() {
  pygmentize -S default -f html
}

gen-html() {
  local input=$1
  local style=$2
  echo '<html>'
  echo '<head>'
  echo "  <link rel=stylesheet type=text/css href=$style />"
  echo '</head>'
  echo '<body>'
  pygmentize -f html -l python $input
  echo '</body>'
  echo '</html>'
}

list-styles() {
  ls /home/andy/hg/pygments/pygments/styles/*.py \
    | grep -v __init__ \
    | grepy '/([^/]+)\.py' -t '$1'
}

out=_tmp/code

write-style() {
  local style=$1
  pygmentize -S $style -f html > $out/$style.css
}

write-styles() {
  list-styles | xargs -n1 -I{} -- $0 write-style {}
  cd $out && ( ls *.css > CSS )
}

list-files() {
  ls ../annex/*.py \
    | grep -v __init__ \
    > $out/HTML
}

gen-all() {
  paste _tmp/code/CSS _tmp/code/HTML | while read css py; do
    echo $css
    echo $py
    local name=$(basename $py)
    local html_out=$out/$name.html
    gen-html $py $css >> $html_out
  done
}

highlight-code() {
  set -o nounset

  mkdir $out
  for f in ../annex/cre*.py; do
    local name=$(basename $f)
    echo $name
    local html_out=$out/$name.html
    gen-html $f $style >> $html_out
  done
  tree $out
}

"$@"

